﻿#region Using
using System;
using System.IO;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using JigLibX.Physics;
using JigLibX.Geometry;
using JigLibX.Collision;
using JigLibX.Math;
#endregion Using

namespace LloydGame
{
    public class GlowParticleSystem : ParticleSystem
    {
        #region Constructor
        public GlowParticleSystem(Game p_game, int p_nrParticles, Vector3 p_position, bool p_respawnParticles)
            : base(p_game, p_nrParticles, p_position, p_respawnParticles)
        {
            m_particleTexture = Game.Content.Load<Texture2D>("Textures\\Particle_Glow");
        }
        #endregion

        #region Update
        protected override void updateParticle(int p_nr, GameTime p_gameTime)
        {
            float dt = ((float)p_gameTime.ElapsedRealTime.Milliseconds) / 1000.0f;

            m_particles[p_nr].swayXAngle += dt * (float)Math.PI;
            m_particles[p_nr].swayZAngle += dt * (float)Math.PI * 1.5f;

            if(m_particles[p_nr].owner != null)
                m_particles[p_nr].position = m_particles[p_nr].owner.position;
        }

        protected override void setupShape(int p_nr)
        {
            Vector3 particleViewPos = Vector3.Transform(m_particles[p_nr].position, ((Game1)Game).camera.view);

            float angle = (m_particles[p_nr].energy > 0.5f ? m_particles[p_nr].swayXAngle / 2 : m_particles[p_nr].swayZAngle / 2);

            Matrix transf = Matrix.CreateFromAxisAngle(
                Vector3.UnitZ * (m_particles[p_nr].spinClockwise ? 1 : -1),
                angle);

            transf *= Matrix.CreateScale((float)Math.Sin(angle / 2) * 0.25f + 1f);

            Vector3 v1 = new Vector3(-m_particles[p_nr].size, m_particles[p_nr].size, 0);
            v1 = Vector3.Transform(v1, transf);

            Vector3 v2 = new Vector3(m_particles[p_nr].size, m_particles[p_nr].size, 0);
            v2 = Vector3.Transform(v2, transf);

            Vector3 v3 = new Vector3(m_particles[p_nr].size, -m_particles[p_nr].size, 0);
            v3 = Vector3.Transform(v3, transf);

            Vector3 v4 = new Vector3(-m_particles[p_nr].size, -m_particles[p_nr].size, 0);
            v4 = Vector3.Transform(v4, transf);

            Color color = new Color(
                0.75f + (float)Math.Sin(angle / 2) * 0.25f,
                0.75f + (float)Math.Sin(angle / 4) * 0.25f,
                0.75f + (float)Math.Sin(angle / 6) * 0.25f, 0.75f);

            // setup shape vertices in view space
            m_shapes[p_nr * 4].Position = v1 + particleViewPos;
            m_shapes[p_nr * 4].Color = color;

            m_shapes[p_nr * 4 + 1].Position = v2 + particleViewPos;
            m_shapes[p_nr * 4 + 1].Color = color;

            m_shapes[p_nr * 4 + 2].Position = v3 + particleViewPos;
            m_shapes[p_nr * 4 + 2].Color = color;

            m_shapes[p_nr * 4 + 3].Position = v4 + particleViewPos;
            m_shapes[p_nr * 4 + 3].Color = color;
        }

        protected override void setParticleDefaults(Particle p_particle, Vector3 p_particleDir)
        {
            p_particle.position = p_particle.owner.position;

            p_particle.size = 2f;

            p_particle.swayXAngle = (float)(((Game1)Game).random.NextDouble() * Math.PI) * 2;
            p_particle.swayZAngle = (float)(((Game1)Game).random.NextDouble() * Math.PI) * 2;

            p_particle.energy = (float)((Game1)Game).random.NextDouble();

            p_particle.spinClockwise = ((Game1)Game).random.NextDouble() > 0.5f;
        }
        #endregion
    }
}
